package com.wuyan.locationdog.ui.my

import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_team_refresh
import com.arcsoft.commonbase.ext.createEmptyView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.TeamListAdapter
import com.wuyan.locationdog.databinding.TeamListActivityBinding
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class TeamListActivity : BaseBindingActivity<TeamListActivityBinding>() {
    private val mViewModel: DeviceViewModel by viewModels()

    private val mAdapter by lazy { TeamListAdapter(R.layout.item_team, null) }

    override fun initData() {

        setTitleText("团队列表")

        binding.apply {
            btnAdd.setOnClickListener {
                ActivityUtils.startActivity(TeamCreateInputActivity::class.java)
            }

            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("暂无团队", com.arcsoft.commonbase.R.mipmap.empty_default, "", "立即创建") {
                        btnAdd.callOnClick()
                    })

                    setOnItemClickListener { adapter, view, position ->
                        val bundle = Bundle()
                        bundle.putSerializable("bean", data[position])
                        ActivityUtils.startActivity(bundle,TeamDetailActivity::class.java)

                    }

                }
            }
            btnDelete.setOnClickListener {

            }

            setOnRefreshLoadMore(smartRefreshLayout)

        }
        loadNetData()


    }

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
        map["pageSize"] = 9999
        map["pageNum"] = 1
        mViewModel.getTeamList(map)
    }

    override fun observe() {
        super.observe()
        Bus.observe<String>(bus_team_refresh, this) {
            loadNetData()
        }
        mViewModel.mTeamDetailList.observe(this) {
            mAdapter.setNewInstance(it?.content)
        }

    }
}